(function () {
    'use strict';

    angular
        .module('app.PatientDemo')
        .controller('PatientDemoController', PatientDemoController);

    PatientDemoController.$inject = ['logger', '$state', '$rootScope', '$mdDialog','$window'];
    /* @ngInject */
    function PatientDemoController(logger, $state, $rootScope, $mdDialog, $window) {
        var vm = this;
        vm.title = 'Patient Demographics';
        vm.user = "";
        vm.password = "";
        vm.ErrorMessage = "";
        vm.EditPatientDetails = EditPatientDetails;
        vm.EditReferralDetails = EditReferralDetails;
        vm.EditDiagnosis = EditDiagnosis;
        vm.EditAttachments = EditAttachments;
        vm.GoToTop = GoToTop;
        activate();
        
        function activate() {

            if($rootScope.isAuthenticated)
            {
                var EditScope = $rootScope.EditScope;
                if (EditScope == 'Diagnosis')
                {
                    EditDiagnosis();
                }
                else if (EditScope == 'Attachments') {
                    EditAttachments();
                }
                else if (EditScope == 'New') {

                    //First Showing Patient Demo modal
                    $mdDialog.show({
                        controller: 'PatientDetailsController',
                        templateUrl: 'app/PatientDetails/PatientDetails.html',
                        parent: angular.element(document.body),
                        controllerAs: 'vm',
                        clickOutsideToClose: false,
                        escapeToClose: false,
                        fullscreen: true
                    }).then(function (answer) {
                        //Form Saved - Show the Referral Details Form
                        $mdDialog.show({
                            controller: 'ReferralDetailsController',
                            templateUrl: 'app/ReferralDetails/ReferralDetails.html',
                            parent: angular.element(document.body),
                            controllerAs: 'vm',
                            clickOutsideToClose: false,
                            escapeToClose : false,
                            fullscreen: true
                        }).then(function (answer) {

                        }, function () {
                            //Form Cancelled - Check if they intened to cancel. If so then Delete Patient record
                            $mdDialog.show(
                               $mdDialog.confirm()
                              .title('Cancel Referral?')
                              .textContent('The referral has not been saved. Cancelling will not create a new referral. Are you sure?')
                              .ariaLabel('Cancel Confirm')
                              .ok('Yes')
                              .cancel('No')
                            )
                            .then(
                                //Yes Function
                                function () {
                                    alert('delete record');
                                }
                                //No Function 
                                , function () {
                                    alert('Somehow show the referral screen again');
                                }
                            );
                        });
                    }, function () {
                        //Form Cancelled - Go One step back
                    });
                }
                //$state.go('dashboard');
            }
        }

        
        function EditPatientDetails() {
            $mdDialog.show({
                controller: 'PatientDetailsController',
                templateUrl: 'app/PatientDetails/PatientDetails.html',
                parent: angular.element(document.body),
                controllerAs: 'vm',
                clickOutsideToClose: false,
                escapeToClose :false,
                fullscreen: true
            }).then(function (answer) {
                
            }, function () {

            });
        }

        function EditReferralDetails() {
            $mdDialog.show({
                controller: 'ReferralDetailsController',
                templateUrl: 'app/ReferralDetails/ReferralDetails.html',
                parent: angular.element(document.body),
                controllerAs: 'vm',
                clickOutsideToClose: false,
                escapeToClose: false,
                fullscreen: true
            }).then(function (answer) {
            }, function () {

            });
        }

        function EditDiagnosis() {
            $mdDialog.show({
                controller: 'DiagnosisController',
                templateUrl: 'app/Diagnosis/Diagnosis.html',
                parent: angular.element(document.body),
                controllerAs: 'vm',
                clickOutsideToClose: false,
                escapeToClose: false,
                fullscreen: true
            }).then(function (answer) {
            }, function () {
                
            });
        }

        function EditAttachments() {
            $mdDialog.show(
                $mdDialog.alert()
                .parent(angular.element(document.body))
                .clickOutsideToClose(true)
                .title('Edit Attachments')
                .textContent('This will have the file upload control')
                .ariaLabel('Alert Dialog Demo')
                .ok('Got it!')
            );
        }

        function GoToTop() {
            $window.scrollTo(0, 0);
        }

        function showWait() {
            $mdDialog.show({
                controller: 'PatientDemoController',
                template: '<md-dialog style="background-color:transparent;box-shadow:none">' +
                            '<div layout="row" layout-sm="column" layout-align="center center" aria-label="wait">' +
                                '<md-progress-circular md-mode="indeterminate" class="md-accent"></md-progress-circular>' +
                            '</div>' +
                         '</md-dialog>',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                fullscreen: false
            })
            .then(function (answer) {

            });
        };
    }
})();


